﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Whir_System_User_LeftMenu : System.Web.UI.Page
{
    protected string menuBranchesCode = "";
    protected string menuLeavesCode = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (!IsPostBack)
        {
            //string xmlPath = Server.MapPath(Whir.Common.Settings.GetString("XmlPath") + "SiteConfig.xml");
            string xmlPath = Server.MapPath("~"+Whir.Common.Settings.GetString("XmlPath") + "SiteConfig.xml");



            Type type = typeof(Whir.Config.Model.SiteConfig);
            Whir.Config.Model.SiteConfig model = (Whir.Config.Model.SiteConfig)Whir.Common.XmlHelper.DeserializeObject(xmlPath, type);         //反序列化

            //litLogoImg.Text = String.Format("<img src='{0}' width='191' height='68' />", Whir.Common.Settings.GetPicServer() + model.logoUrl);
            litLogoImg.Text = String.Format("<img src='{0}' width='191' height='68' />", ResolveClientUrl("~" + Whir.Common.Settings.GetPicServer() + model.logoUrl));


            BindBranches();
            BindLeaves();
        }
    }

    private static string OpenColumnManage = "{id:'OpenColumnManage',text:'栏目管理',handler:function(){window.open(\"Module/Role/ColumnList.aspx\",\"mainFrame\");}}";
    private static string PublishAllColumn = "{id:'PublishAllColumn',text:'栏目发布',handler:function(){window.open(\"Module/Publish/PublishManage.aspx\",\"mainFrame\");}}";
    private static string EditAllModules = "{id:'EditAllModules',text:'模板库',handler:function(){window.open(\"Module/Template/TemplateList.aspx\",\"mainFrame\");}}";
    private static string EditAllColumnTemplates = "{id:'EditAllColumnTemplates',text:'栏目模板对应表',handler:function(){window.open(\"Module/Template/ColumnTemplateList.aspx\",\"mainFrame\");}}";

    #region 预览
    private static string PreviewItem =
           "{" +
             "   id:'PreviewItem'," +
               " text:'预览'," +
                "handler:function(){" +
                  "  if(selected.leaf){" +
                    "    window.open(\"UserControls/Common/go.ashx?Action=PreviewItem&columnID=\" + selected.id);" +
                    "}else{" +
                    "    window.parent.alert('当前选中为根节点，请选择预览子节点');" +
                    "}" +
                "}" +
           "}";
    #endregion

    #region 发布
    private static string PublishSelected =
        "{" +
            "    id:'PublishSelected'," +
             "   text : '发布'," +
              "  handler:function (){" +
               "     if(selected.id == 1 || selected.id == \"source\"){" +
                "        document.getElementById('hideFrame').src = 'Module/Publish/publish.ashx?columnID=-1';" +
                 "       window.parent.alert(\"所有栏目发布成功\");" +
                  "  }else if(selected.leaf){" +
                   "     document.getElementById('hideFrame').src = 'Module/Publish/publish.ashx?columnID=' + selected.id;" +
                    "    window.parent.alert(\"[ \" + selected.text + \" ]栏目发布成功\");" +
                    "}else{" +
                    "    window.parent.alert('请选择要发布的栏目');" +
                   " }" +
                "}" +
            "}";
    #endregion

    #region 当前栏目模板
    private static string EditModules =
            "{" +
             "  id:'EditModules'," +
              " text:'当前模板'," +
               "handler:function (){" +
                "   if(selected.leaf){" +
                 "       window.open(\"UserControls/Common/go.ashx?Action=EditModules&columnID=\" + selected.id);" +
                  " }else{" +
                   "     window.parent.alert(\"当前选中为根节点，请选择子节点\");" +
                   "}" +
               "}" +
             "}";
    #endregion

    #region 回收站
    private static string Recyclebin =
        "{" +
             "  id:'Recyclebin'," +
              " text:'回收站'," +
               "handler:function (){" +
                "   if(selected.leaf){" +
                 "       var url = \"UserControls/Common/go.ashx\";" +
                   "     Ext.Ajax.request({" +
                    "        url : url , " +
                     "       params : { Action : 'Recyclebin' ,columnID : selected.id}," +
                      "      method: 'GET'," +
                       "     success: function ( result, request ) { " +
                        "        var s = result.responseText;" +
                         "       if(s == \"Failed\"){" +
                          "          window.parent.alert(\"当前选中的栏目没有回收站\");" +
                           "     }else{" +
                            "        window.open(result.responseText + \"&a=\" + Math.random(),\"mainFrame\");" +
                             "   }" +
                            "}," +
                            "failure: function ( result, request) { " +
                             "   window.parent.alert('Failed'); " +
                            "} " +
                        "});" +
                   "}" +
               "}" +
            "}";
    #endregion

    #region 重命名
    private static string ReName =
        "{" +
          "          id:'ReNameColumn'," +
          "          text:'重命名'," +
          "         icon:'image/CommonImage/tree_edit.gif'," +
           "         handler:function(){" +
            "            var newname = prompt(\"请输入新栏目名称\",\"\");" +
             "           if(newname!=null && newname!=\"\"){" +
              "              Ext.Ajax.request({" +
               "                     url: 'UserControls/Common/go.ashx?Action=ReNameColumn&columnID='+selected.id+\"&editItemvalue=\"+escape(newname)," +
                "                    success:function(request){" +
                 "                       tree.root.reload();                         " +
                  "                  },failure:function(){" +
                   "                     window.parent.alert('栏目重命名操作失败！');" +
                    "                }" +
                     "        });    " +
                      "  } " +
                    "}" +
                "}";
    #endregion

    #region 删除
    private static string DeleteName =
        "{" +
          "          id:'DeleteColumn'," +
          "          text:'删除'," +
           "         icon:'image/CommonImage/tree_delete.gif'," +
           "         handler:function(){" +
            "            if(confirm('是否删除“'+selected.text+'”栏目？一旦删除，将不可恢复！')) {" +
              "              Ext.Ajax.request({" +
               "                     url: 'UserControls/Common/go.ashx?Action=DeleteColumn&columnID='+selected.id," +
                "                    success:function(request){" +
                 "                       tree.root.reload();                         " +
                  "                  },failure:function(){" +
                   "                     window.parent.alert('栏目重命名操作失败！');" +
                    "                }" +
                     "        });    " +
                      "  } " +
                    "}" +
                "}";
    #endregion

    #region 添加子栏目
    private static string AddChildColumn =
        "{" +
          "          id:'AddChildColumn'," +
          "          text:'添加子栏目'," +
          "          icon:'image/CommonImage/tree_add.gif'," +
           "         handler:function(){" +
            "              var newname = prompt(\"请输入当前所选择的栏目下的子栏目名称\",\"\");" +
               "           if(newname!=null && newname!=\"\"){" +
                  "              Ext.Ajax.request({" +
                   "                     url: 'UserControls/Common/go.ashx?Action=AddChildColumn&parentID='+selected.id+\"&ChildItemvalue=\"+escape(newname)," +
                    "                    success:function(request){" +
                     "                       tree.root.reload();                         " +
                      "                  },failure:function(){" +
                       "                     window.parent.alert('添加子栏目失败！');" +
                        "                }" +
                         "        });    " +
                          "  } " +
                    "}" +
                "}";
    #endregion

    private void BindBranches()
    {
        if (RoleHelper.AdminCache.createUserID == 0)
            menuBranchesCode = OpenColumnManage + "," + PublishAllColumn + "," + EditAllModules + "," + EditAllColumnTemplates + ",'-'," + AddChildColumn + "," + ReName;
        else
            menuBranchesCode = PublishAllColumn;

    }

    private void BindLeaves()
    {
        if (RoleHelper.AdminCache.createUserID == 0)
            menuLeavesCode = PreviewItem + ",'-'," + PublishSelected + "," + EditModules + ",'-'," + AddChildColumn + "," + ReName;// + "," + DeleteName;
        else
            menuLeavesCode = PreviewItem + "," + PublishSelected;
    }
}
